package com.xiaoyi.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avin.yi.R;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.ui.fragment.draw.DrawFragment;
import com.xiaoyi.ui.fragment.music.MusicFragment;
import com.xiaoyi.ui.fragment.pic.PicFragment;
import com.xiaoyi.ui.fragment.pic.PicDetailFragment;
import com.xiaoyi.ui.fragment.vedio.VedioFragment;
import com.xiaoyi.ui.view.BottomBar;
import com.xiaoyi.ui.view.BottomBarTab;

import org.greenrobot.eventbus.Subscribe;

import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;



public class MainFragment extends SupportFragment  {
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOUR = 3;
    private SupportFragment[] mFragments = new SupportFragment[4];
    private BottomBar mBottomBar;


    public static MainFragment newInstance() {

        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBusActivityScope.getDefault(_mActivity).register(this);
        View view = inflater.inflate(R.layout.zhihu_activity_main, container, false);

        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment firstFragment = findChildFragment(PicFragment.class);
        if (firstFragment == null) {
            mFragments[FIRST] = PicFragment.newInstance();
            mFragments[SECOND] = MusicFragment.newInstance();
            mFragments[THIRD] = VedioFragment.newInstance();
            mFragments[FOUR] = DrawFragment.newInstance();
            loadMultipleRootFragment(
                    R.id.fl_container,
                    FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD] ,
                    mFragments[FOUR]
                    );
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findFragment(MusicFragment.class);
            mFragments[THIRD] = findFragment(VedioFragment.class);
            mFragments[FOUR] = findFragment(DrawFragment.class);
        }
    }

    private void initView(View view) {
        mBottomBar = view.findViewById(R.id.bottomBar);
        mBottomBar
                .addItem( new BottomBarTab(_mActivity, R.drawable.pic ,"识图")  )
                .addItem( new BottomBarTab(_mActivity, R.drawable.music ,"音乐"))
                .addItem( new BottomBarTab( _mActivity, R.drawable.vedio ,"视频"))
                .addItem( new BottomBarTab( _mActivity, R.drawable.draw ,"画画"))
        ;

        mBottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
                /// AvBaseTool.showMsg(_mActivity ,"重复点击："+position);
            }
        });
    }

    /**
     *  在main 页面开启新的兄弟fragment
     */
    @Subscribe
    public void onTest(SwipeBackFragment fragment) {
        AvBaseTool.d("-----------启动。。。");
        if (fragment instanceof PicDetailFragment) {
            startBrotherFragment(fragment);
        }

    }


    /**
     * start other BrotherFragment
     */
    public void startBrotherFragment(SupportFragment targetFragment) {
        start(targetFragment);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusActivityScope.getDefault(_mActivity).unregister(this);
    }


    public  void gotoFirstFragment(){
         mBottomBar.setCurrentItem(0);
    }
}
